//关联用户
const express = require("express");
const router = express.Router();
const db = require("../utils/db");

//渲染数据
router.get("/associatedget", (req, res) => {
  const { search } = req.query;
  const sql = `select * from associatedUsers;`;
  const sql2 = `select count(*) as total from associatedUsers where username like '%${search}%';`;
  db.query(sql, (err, data) => {
    // console.log(data);
    if (err) {
      res.json({ error: 1, msg: err });
    } else {
      db.query(sql2, (err2, data2) => {
        if (err2) {
          res.json({ error: 1, msg: err2 });
        } else {
          // console.log(data2)
          res.json({ error: 0, data: data, total: data2[0].total });
        }
      });
    }
  });
});

//高级筛选
router.post("/advancedFilter", (req, res) => {
  console.log(req.body);
  const { nickname, username, address, roleType } = req.body;
  const sql = `SELECT * FROM associatedUsers  
    WHERE nickname LIKE '%${nickname}%' AND username LIKE '%${username}%' AND address LIKE '%${address}%' AND roleType LIKE '%${roleType}%';`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});

//删除
router.post("/associatdel", (req, res) => {
  console.log(req.body);
  const { key } = req.body;
  const sql = `delete from associatedUsers where key_a=${key};`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});

//编辑
router.post("/associatEdit", (req, res) => {
  console.log(req.body);
  const { key_a, roleName } = req.body;
  const sql = `update associatedUsers set roleName='${roleName}' where key_a='${key_a}'`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});

//获取页面左侧的角色的内容
router.post("/getAllRole", (req, res) => {
  console.log(req.body);
  const sql = `select * from allrole;`;
  const sql1 = `SELECT id,roleContent FROM allrole `;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      db.query(sql1, (err1, data1) => {
        if (err1) {
          res.send({ error: 1, msg: err1 });
        } else {
          res.send({ error: 0, data: data, content: data1 });
        }
      });
    }
  });
});

//删除页面左侧的角色的内容
router.post("/delRoleData", (req, res) => {
  console.log(req.body);
  const { roleContent,id } = req.body;
  const sql = `UPDATE allrole SET roleContent='${roleContent}' WHERE id='${id}'`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});

//添加页面左侧的角色的内容
router.post("/addRoleData", (req, res) => {
  console.log(req.body);
  const { roleContent,roleTitle } = req.body;
  const sql = `UPDATE allrole SET roleContent='${roleContent}' WHERE roleTitle='${roleTitle}'`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});



//点击li 渲染右边的表
router.post("/oLiClick", (req, res) => {
  console.log(req.body);
  const { roleName } = req.body;
  const sql = `select * from associatedUsers where roleName like '%${roleName}%'`;
  db.query(sql, (err, data) => {
    console.log(data);
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});



module.exports = router;
